import settlementApi from '@/api/settlement/settlement'
import attrKeyApi from '@/api/shop/attrKey'
import attrValApi from '@/api/shop/attrVal'
import { getApiUrl } from '@/utils/utils'
import permission from '@/directive/permission/index.js'
export default {
  directives: { permission },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 20,
        id: undefined
      },
      total: 0,
      list: null,
      listLoading: true,
      goodsCreatList: [],
      goodsCreatSelect: 0,
      startTime: new Date(),
      endTime: new Date(),
      timeChange: [this.startTime, this.endTime],
      timeFrom: "",
      timeTo: "",
      initFlag: true,
    }
  },
  watch: {
    goodsCreatSelect(val, oldVal) {
      //console.log('筛选框值变动', val, oldVal);
      this.fetchData(val)
    },
    timeChange(val, oldVal) {
      if (val == null) {
        this.timeFrom = ""
        this.timeTo = ""
      } else {
        this.timeFrom = val[0]
        this.timeTo = val[1]
      }
      console.log('时间', val, oldVal);

      this.fetchData(this.goodsCreatSelect)
    }
  },
  created() {
    this.init()
    this.initFlag = true
  },
  methods: {
    init() {
      this.initDate()
      this.getGoodsCreatList()
      // this.fetchData(0)
    },
    fetchData(goodsCreatId) {

      this.listLoading = true
      let tmpReqData = {
        "goodsCreat": goodsCreatId,
        "dateFrom": this.timeFrom,
        "DateTo": this.timeTo,
      }
      settlementApi.queryWriteOffList(tmpReqData).then(response => {
        //console.log("已核销的数据", response);
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },
    search() {
      this.listQuery.page = 1
      this.fetchData()
    },
    reset() {
      this.listQuery.id = ''
      this.listQuery.page = 1
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      this.selRow = currentRow
    },
    getGoodsCreatList() {
      settlementApi.getSysUser().then(response => {
        let a = {
          id: 0,
          nameAndAccount: "全部"
        }
        this.goodsCreatList.push(a)
        //console.log("获取创建者ID", response);
        for (let i = 0; i < response.data.length; i++) {
          const e = response.data[i];
          let tmp = {
            id: e.id,
            nameAndAccount: e.nameAndAccount,
            name: e.name,
          }
          this.goodsCreatList.push(tmp)
        }
        this.goodsCreatSelect = 0
        var goodsCreatId = 0;
        if (goodsCreatId == 0 && this.initFlag) {
          let thisUserId = this.$store.state.user.profile.id
          for (let i = 0; i < this.goodsCreatList.length; i++) {
            const e = this.goodsCreatList[i];
            if (e.id == thisUserId) {
              this.goodsCreatSelect = e.id
              goodsCreatId = e.id
              break
            } else {
              goodsCreatId = 0
            }
          }
          if(goodsCreatId == 0){
            this.fetchData(goodsCreatId)
          }
          this.initFlag = false
        }
      })
    },
    fmoney(s, n) {
      /*
       * 参数说明：
       * s：要格式化的数字
       * n：保留几位小数
       * */
      n = n > 0 && n <= 20 ? n : 2;
      s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
      var l = s.split(".")[0].split("").reverse(),
        r = s.split(".")[1];
      let t = "";
      for (let i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
      }
      return t.split("").reverse().join("") + "." + r;
    },
    formatCreat: function (row, cellValue) {
      // //console.log(JSON.stringify(row))
      let goodsCreat = row.goodsCreat
      let userList = this.goodsCreatList
      for (let item in userList) {
        let id = userList[item].id
        if (id == goodsCreat) {
          return userList[item].name
        }
      }
    },
    initDate() {
      let n = new Date()
      this.timeFrom = n.getTime() - 3600 * 1000 * 24 * 30
      this.timeTo = n.getTime()
      // console.log("this.timeFrom", this.timeFrom);
      // console.log("this.timeTo", this.timeTo);
      // console.log("this.timeChange", this.timeChange);
      this.timeChange[0] = this.timeFrom
      this.timeChange[1] = this.timeTo

    }
  }
}
